<template>
  <div class="container">
    <div class="video-container">
      <div v-if="isVideoAvailable" class="video-wrapper">
        <img class="video-stream" :src="videoSrc" style="float: left;" />
      </div>
      <div v-else class="video-placeholder">
        <div >
          <div class="loader"></div>
          <h2 style="color: white">Loading...</h2>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import {invade_close_click, monitor_close_click, register_close_click, track_close_click} from "@/api/close";
import {ElMessage} from "element-plus";
import {ref, onMounted} from "vue";
import {globalVariable} from "@/store/modules/globalurl";

onMounted(async () => {
  await close(); // Wait for close to finish
  checkVideoAvailability(); // Then check video availability
});

async function close() {
  try {
    const responses = await Promise.all([
      register_close_click(),
      invade_close_click(),
      track_close_click(),
      monitor_close_click()
    ]);
    // Handle success for all requests
    responses.forEach(response => {
      var res = response.data;
      if (res.respCode !== '000000') {
        ElMessage.error(res.respMsg);
      }
    });
  } catch (error) {
    console.error(error);
    ElMessage.error('关闭失败，请重试');
  }
}


const videoSrc = ref(globalVariable+'/api/video_feed/');
const isVideoAvailable = ref(false);

const checkVideoAvailability = async () => {
  try {
    const response = await fetch(videoSrc.value);
    if (response.status === 200) {
      isVideoAvailable.value = true;
    } else {
      console.error('获取视频1信息失败，状态码：', response.status);
    }
  } catch (error) {
    console.error('发生错误：', error);
  }
};


</script>

<style scoped>
.controls {
  display: flex;
  justify-content: center;
  align-items: center;
}

.video-container {
  display: grid;
  place-items: center;
  height: 100%;
}

.container {
  display: flex;
  justify-content: center;
  align-items: center;
}

.video-stream {
  width: 100%;
}

.video-placeholder {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 750px;
  height: 500px;
  background-color: #3c5287;
  color: #333;
  font-size: 1.2em;
}

.loader {
  -webkit-animation: spin 1s linear infinite;
  animation: spin 1s linear infinite;
  border: 30px solid #ddd;
  border-bottom: 30px solid #64b5f6;
  border-radius: 50%;
  width: 100px;
  height: 100px;
}

@-webkit-keyframes spin {
  0% {
    -webkit-transform: rotate(0deg);
  }
  100% {
    -webkit-transform: rotate(-360deg);
  }
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(-360deg);
  }
}

h2 {
  animation: typing 1s infinite;
  font-size: 30px;
  margin-top: 30px;
  overflow: hidden;
}

@keyframes typing {
  from {
    width: 70%;
  }
  to {
    width: 100%;
  }
}

</style>